package com.hc.controller;

import com.hc.entity.Incubatormv;
import com.hc.entity.QcEquipmentRecord;
import com.hc.model.*;
import com.hc.service.QcEquipmentRecordExcelService;
import com.hc.service.QcEquipmentRecordService;
import com.hc.service.TriplicitiesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author LiuZhiHao
 * @date 2019/8/28 13:48
 * 描述:
 **/
@RestController
@RequestMapping("/qcEquipmentRecord")
@Api(description = "巡检记录")
public class QcEquipmentRecordController {


    @Autowired
    private QcEquipmentRecordService qcEquipmentRecordService;

    @Autowired
    private QcEquipmentRecordExcelService qcEquipmentRecordExcelService;

    @Autowired
    private TriplicitiesService triplicitiesService;

    private static final DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ofPattern("HH:mm");



    @PostMapping("")
    @ApiOperation("新增巡检设备记录")
    public void saveQcEquipmentRecord(@RequestBody QcEquipmentRecordModel qcEquipmentRecordModel) {
        qcEquipmentRecordService.saveQcEquipmentRecord(qcEquipmentRecordModel);
    }

    @PostMapping("/inspectionStatus")
    @ApiOperation("获取巡检完成状态")
    public List<QcRecordStatus> getinspectionStatus(@RequestBody QcRecordStatusModel qcRecordStatusModel) {
        return qcEquipmentRecordService.getinspectionStatus(qcRecordStatusModel);
    }


    @PostMapping("/workStatus")
    @ApiOperation("获取工作台工作状态")
    public List<QcRecordStatus> getWorkStatus(@RequestBody QcRecordStatusModel qcRecordStatusModel) {
        return qcEquipmentRecordService.getWorkStatus(qcRecordStatusModel);
    }

    @GetMapping("/workbenchOperationBatch")
    @ApiOperation("操作批次查询(只查一天)")
    public QcEquipmentRecord getWorkbenchOperationBatch(String eqId, String workType, String date) {
        return qcEquipmentRecordService.getWorkbenchOperationBatch(eqId, workType, date);
    }

    @PostMapping("/findEqDeatil")
    @ApiOperation("查询设备巡检记录详情")
    public List<QcEquipmentRecordModel> findEqDeatil(@RequestBody QcRecordStatusModel qcRecordStatusModel) {
        return qcEquipmentRecordService.findEqDeatil(qcRecordStatusModel);
    }

    @GetMapping("/exportExcel")
    @ApiOperation("导出巡检记录excel")
    public void exportExcel(HttpServletResponse response, QcRecordStatusModel qcRecordStatusModel) {
        qcEquipmentRecordExcelService.exportExcel(response, qcRecordStatusModel);
    }

    @GetMapping("/getQualityControlReportEveryDayForBX")
    @ApiOperation("获取冰箱每日质控报表数据")
    public List<QualityControlReportList> getQualityControlReportEveryDayForBX(@RequestParam("date") String date,
                                                                               @RequestParam("userId") String userId) {
        return qcEquipmentRecordService.getQualityControlReportEveryDayForBX(date, userId);
    }

    @GetMapping("/getQualityControlReportEveryMonthForBX")
    @ApiOperation("获取冰箱每月质控报表数据")
    public List<QualityControlReportList> getQualityControlReportEveryMonthForBX(@RequestParam("eqId") int eqId,
                                                                                 @RequestParam("startTime") String startTime,
                                                                                 @RequestParam("endTime") String endTime) {
        return qcEquipmentRecordService.getQualityControlReportEveryMonthForBX(eqId, startTime, endTime);
    }

    @GetMapping("/getQualityControlReportEveryDayForPYX")
    @ApiOperation("获取培养箱每日质控报表数据")
    public List<QualityControlReportList> getQualityControlReportEveryDayForPYX(@RequestParam("date") String date,
                                                                                @RequestParam("userId") String userId) {
        return qcEquipmentRecordService.getQualityControlReportEveryDayForPYXNew(date, userId);
    }

    @GetMapping("/getQualityControlReportEveryMonthForPYX")
    @ApiOperation("获取培养箱每月质控报表数据")
    public List<QualityControlReportList> getQualityControlReportEveryMonthForPYX(@RequestParam("eqId") int eqId,
                                                                                  @RequestParam("startTime") String startTime,
                                                                                  @RequestParam("endTime") String endTime) {
        return qcEquipmentRecordService.getQualityControlReportEveryMonthForPYX(eqId, startTime, endTime);
    }

    @GetMapping("/getQualityControlReportEveryDayForXWJ")
    @ApiOperation("获取显微镜每日质控报表数据")
    public List<QualityControlReportList> getQualityControlReportEveryDayForXWJ(@RequestParam("date") String date,
                                                                                @RequestParam("userId") String userId) {
        return qcEquipmentRecordService.getQualityControlReportEveryDayForXWJ(date, userId);
    }

    @GetMapping("/getQualityControlReportEveryMonthForXWJ")
    @ApiOperation("获取显微镜每月质控报表数据")
    public List<QualityControlReportList> getQualityControlReportEveryMonthForXWJ(@RequestParam("eqId") int eqId,
                                                                                  @RequestParam("startTime") String startTime,
                                                                                  @RequestParam("endTime") String endTime) {
        return qcEquipmentRecordService.getQualityControlReportEveryMonthForXWJ(eqId, startTime, endTime);
    }

    @GetMapping("/getQualityControlReportEveryDayForLXJOrGZZ")
    @ApiOperation("获取离心机或者工作站每日质控报表数据")
    public List<QualityControlReportList> getQualityControlReportEveryDayForLXJOrGZZ(@RequestParam("date") String date,
                                                                                     @RequestParam("userId") String userId,
                                                                                     @RequestParam("classifyId") String classifyId) {
        return qcEquipmentRecordService.getQualityControlReportEveryDayForLXJOrGZZ(date, userId, classifyId);
    }

    @GetMapping("/getQualityControlReportEveryMonthForLXJOrGZZ")
    @ApiOperation("获取离心机或者工作站每月质控报表数据")
    public List<QualityControlReportList> getQualityControlReportEveryMonthForLXJOrGZZ(@RequestParam("eqId") int eqId,
                                                                                       @RequestParam("startTime") String startTime,
                                                                                       @RequestParam("endTime") String endTime) {
        return qcEquipmentRecordService.getQualityControlReportEveryMonthForLXJOrGZZ(eqId, startTime, endTime);
    }

    @GetMapping("/getQualityControlReportEveryDayForPTK")
    @ApiOperation("获取胚胎库每日质控报表数据")
    public List<QualityControlReportList> getQualityControlReportEveryDayForPTK(@RequestParam("date") String date,
                                                                                @RequestParam("userId") String userId) {
        return qcEquipmentRecordService.getQualityControlReportEveryDayForPTK(date, userId);
    }

    @GetMapping("/getQualityControlReportEveryMonthForPTK")
    @ApiOperation("获取胚胎库每月质控报表数据")
    public List<QualityControlReportList> getQualityControlReportEveryMonthForPTK(@RequestParam("eqId") int eqId,
                                                                                  @RequestParam("startTime") String startTime,
                                                                                  @RequestParam("endTime") String endTime) {
        return qcEquipmentRecordService.getQualityControlReportEveryMonthForPTK(eqId, startTime, endTime);
    }

    @GetMapping("/getQualityControlReportEveryDayForHJ")
    @ApiOperation("获取环境每日质控报表数据")
    public List<QualityControlReportList> getQualityControlReportEveryDayForHJ(@RequestParam("date") String date,
                                                                               @RequestParam("userId") String userId) {
        return qcEquipmentRecordService.getQualityControlReportEveryDayForHJ(date, userId);
    }

    @GetMapping("/getQualityControlReportEveryMonthForHJ")
    @ApiOperation("获取环境每月质控报表数据")
    public List<QualityControlReportList> getQualityControlReportEveryMonthForHJ(@RequestParam("eqId") int eqId,
                                                                                 @RequestParam("startTime") String startTime,
                                                                                 @RequestParam("endTime") String endTime) {
        return qcEquipmentRecordService.getQualityControlReportEveryMonthForHJ(eqId, startTime, endTime);
    }

    @GetMapping("/getQualityControlReportEveryDayForGZT")
    @ApiOperation("获取工作台每日质控报表数据")
    public List<WorkbenchModel> getQualityControlReportEveryDayForGZT(@RequestParam("date") String date,
                                                                      @RequestParam("userId") String userId) {
        return qcEquipmentRecordService.getWorkbenchModelEveryDayForGZT(date, userId);
    }

    @GetMapping("/getWorkbenchModelEveryMonthForGZT")
    @ApiOperation("获取工作台每月质控报表数据")
    public List<WorkbenchModel> getWorkbenchModelEveryMonthForGZT(@RequestParam("eqId") int eqId,
                                                                  @RequestParam("startTime") String startTime,
                                                                  @RequestParam("endTime") String endTime) {
        return qcEquipmentRecordService.getWorkbenchModelEveryMonthForGZT(eqId, startTime, endTime);
    }

    @GetMapping("/excelBXForMonth")
    @ApiOperation("冰箱excel导出")
    public void excelBXForMonth(HttpServletResponse response,
                                @RequestParam("eqId") int eqId,
                                @RequestParam("startTime") String startTime,
                                @RequestParam("endTime") String endTime) {
        qcEquipmentRecordExcelService.excelBXForMonth(response, eqId, startTime, endTime);
    }

    @GetMapping("/excelPYXForMonth")
    @ApiOperation("培养箱excel导出")
    public void excelPYXForMonth(HttpServletResponse response,
                                 @RequestParam("eqId") int eqId,
                                 @RequestParam("startTime") String startTime,
                                 @RequestParam("endTime") String endTime) {
        qcEquipmentRecordExcelService.excelPYXForMonth(response, eqId, startTime, endTime);
    }

    @GetMapping("/excelHJForMonth")
    @ApiOperation("环境excel导出")
    public void excelHJForMonth(HttpServletResponse response,
                                @RequestParam("eqId") int eqId,
                                @RequestParam("startTime") String startTime,
                                @RequestParam("endTime") String endTime) {
        qcEquipmentRecordExcelService.excelHJForMonth(response, eqId, startTime, endTime);
    }

    @GetMapping("/excelGZTForMonth")
    @ApiOperation("工作台excel导出")
    public void excelGZTForMonth(HttpServletResponse response,
                                 @RequestParam("eqId") int eqId,
                                 @RequestParam("startTime") String startTime,
                                 @RequestParam("endTime") String endTime) {
        qcEquipmentRecordExcelService.excelGZTForMonth(response, eqId, startTime, endTime);
    }

    @GetMapping("/excelXWJForMonth")
    @ApiOperation("显微镜excel导出")
    public void excelXWJForMonth(HttpServletResponse response,
                                 @RequestParam("eqId") int eqId,
                                 @RequestParam("startTime") String startTime,
                                 @RequestParam("endTime") String endTime) {
        qcEquipmentRecordExcelService.excelXWJForMonth(response, eqId, startTime, endTime);
    }

    @GetMapping("/excelGZZOrLXJForMonth")
    @ApiOperation("工作站和离心机excel导出")
    public void excelGZZOrLXJForMonth(HttpServletResponse response,
                                      @RequestParam("eqId") int eqId,
                                      @RequestParam("startTime") String startTime,
                                      @RequestParam("endTime") String endTime,
                                      @RequestParam("classifyId") String classifyId) {
        qcEquipmentRecordExcelService.excelGZZOrLXJForMonth(response, eqId, startTime, endTime, classifyId);
    }

    @GetMapping("/excelPTKForMonth")
    @ApiOperation("胚胎库excel导出")
    public void excelPTKForMonth(HttpServletResponse response,
                                 @RequestParam("eqId") int eqId,
                                 @RequestParam("startTime") String startTime,
                                 @RequestParam("endTime") String endTime) {
        qcEquipmentRecordExcelService.excelPTKForMonth(response, eqId, startTime, endTime);
    }

    @GetMapping("/excelByClassifyAllForMonth")
    @ApiOperation("根据设备类型获取该类型设备的所有月报表数据导出")
    public void excelByClassifyAllForMonth(HttpServletResponse response,
                                 @RequestParam("classifyId") String classifyId,
                                 @RequestParam("startTime") String startTime,
                                 @RequestParam("endTime") String endTime) {
        qcEquipmentRecordExcelService.excelByClassifyAllForMonth(response, classifyId, startTime, endTime);
    }

    @GetMapping("/getIncubatorPointDataByHosId")
    @ApiOperation("请求监控设备数据")
    public Map<String, List<Incubatormv>> excelByClassifyAllForMonth(
                                           @RequestParam("date") String date,
                                           @RequestParam(value = "hospitalCode",required = false) String hospitalCode,
                                           @RequestParam("equipmenttypeId") String equipmenttypeId) {
        List<Incubatormv> incubatorByDate = triplicitiesService.getIncubatorByDate(date, equipmenttypeId);
        //每个设备只保留00:00 06:00 12:00 18:00 这四个点的数据
        if (CollectionUtils.isNotEmpty(incubatorByDate)){
            List<Incubatormv> collect = incubatorByDate.stream()
                    .filter(item -> {
                        LocalDateTime time = LocalDateTime.parse(item.getInputTime(),
                                DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                        String hourMinute = time.format(TIME_FORMAT);
                        return hourMinute.equals("00:00") ||
                                hourMinute.equals("06:00") ||
                                hourMinute.equals("12:00") ||
                                hourMinute.equals("18:00");
                    })
                    .collect(Collectors.toList());
            return collect.stream().collect(Collectors.groupingBy(Incubatormv::getEquipmentNo));
        }
        return null;
    }

}
